﻿#include "mytablemodel.h"

#include <QBrush>
#include <QColor>

MyTableModel::MyTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{

}

int MyTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 20;
}

int MyTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 10;
}

Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags itemFlags = QAbstractItemModel::flags(index);
    itemFlags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return itemFlags;
}

QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }
    int row = index.row();
    int col = index.column();
    if (role == Qt::DisplayRole)
    {
        return QString("row %1, col %2").arg(row).arg(col);
    }
    else if (role == Qt::BackgroundRole)
    {
        if (row % 3 == 0)
        {
            return QColor(Qt::yellow);
        }
        else
        {
            return QColor(Qt::white);
        }
    }
    else if (role == Qt::ForegroundRole)
    {
        if (row % 3 == 0 && col % 2 == 0)
        {
            return QColor(Qt::red);
        }
        else
        {
            return QColor(Qt::black);
        }
    }
    return QVariant();
}
